/********************************************************************************
 * CurrencyConverterAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Oct 14, 2008 10:27:30 AM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import org.apache.log4j.Logger;

import NET.webserviceX.www.Currency;

import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CurrencyServiceDelegator;
import com.printeverywhere.dto.CurrencyConverterConfiguration;
import com.printeverywhere.util.CurrencyConverter;

public class CurrencyConverterAction extends ActionSupport {

	/**  */
	private static final long serialVersionUID = 1L;

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(PortalToolsAction.class);

	private CurrencyConverterConfiguration converterConfiguration;


	private String convertTo;

	private String convertFrom;

	private String countryTo;

	private String countryFrom;

	private double amount;

	private double conversionRate;

	private double invertedConversionRate;

	private boolean inverted;

	public String getConvertTo() {
		return convertTo;
	}

	public void setConvertTo(String convertTo) {
		this.convertTo = convertTo;
	}

	public String getConvertFrom() {
		return convertFrom;
	}

	public void setConvertFrom(String convertFrom) {
		this.convertFrom = convertFrom;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public double getInvertedConversionRate() {
		return invertedConversionRate;
	}

	public void setInvertedConversionRate(double invertedConversionRate) {
		this.invertedConversionRate = invertedConversionRate;
	}

	public double getConversionRate() {
		return conversionRate;
	}

	public void setConversionRate(double conversionRate) {
		this.conversionRate = conversionRate;
	}

	public boolean isInverted() {
		return inverted;
	}

	public void setInverted(boolean inverted) {
		this.inverted = inverted;
	}

	public String getCountryTo() {
		return countryTo;
	}

	public void setCountryTo(String countryTo) {
		this.countryTo = countryTo;
	}

	public String getCountryFrom() {
		return countryFrom;
	}

	public void setCountryFrom(String countryFrom) {
		this.countryFrom = countryFrom;
	}

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		converterConfiguration = CurrencyServiceDelegator.getConfigurationList();
		convertTo = converterConfiguration.getDefaultCurrencyConvertTo();
		convertFrom = converterConfiguration.getDefaultCurrencyConvertFrom();
		amount = converterConfiguration.getConvertAmount();

		this.conversionRate = CurrencyConverter.conversionRate(Currency.fromString(this.convertFrom), Currency.fromString(this.convertTo));

		if(inverted) {
			this.invertedConversionRate = CurrencyConverter.conversionRate(Currency.fromString(this.convertTo), Currency.fromString(this.convertFrom));
			System.out.println(this.invertedConversionRate);
		}

		try {
			this.conversionRate = this.conversionRate * this.amount;
			this.invertedConversionRate = this.invertedConversionRate * this.amount;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}

		return SUCCESS;
	}

	public String submitCurrencyConverter() {
		this.conversionRate = CurrencyConverter.conversionRate(Currency.fromString(this.convertFrom), Currency.fromString(this.convertTo));

		if(inverted) {
			this.invertedConversionRate = CurrencyConverter.conversionRate(Currency.fromString(this.convertTo), Currency.fromString(this.convertFrom));
			System.out.println(this.invertedConversionRate);
		}

		try {
			this.conversionRate = this.conversionRate * this.amount;
			this.invertedConversionRate = this.invertedConversionRate * this.amount;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}

		return SUCCESS;
	}

	public CurrencyConverterConfiguration getConverterConfiguration() {
		return converterConfiguration;
	}

	public void setConverterConfiguration(
			CurrencyConverterConfiguration converterConfiguration) {
		this.converterConfiguration = converterConfiguration;
	}
}
